package com.tuoyu.peopletest.view.custom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2017/8/2 0002.
 */

public class MyProgressView extends View {

    private int width  = 300;
    private int height = 300;
    private Paint  circlePaint;
    private Paint  progressPaint;
    private Paint  textPaint;
    private Bitmap bitmap;
    private Canvas bitmapCanvas;
    private Path path            = new Path();
    private int  progress        = 50;
    private int  max             = 100;
    private int  currentProgress = 0;
    private int  count           = 50;
    private boolean isSingleTag = false;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

        }
    };

    public MyProgressView(Context context) {
        super(context);
        init();
    }

    public MyProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        circlePaint = new Paint();
        circlePaint.setColor(Color.argb(0xff, 0x3a, 0x8c, 0x6c));
        circlePaint.setAntiAlias(true);
        circlePaint.setDither(true);

        progressPaint = new Paint();
        progressPaint.setColor(Color.argb(0xff, 0x4c, 0xc9, 0x63));
        progressPaint.setAntiAlias(true);
        progressPaint.setStyle(Paint.Style.FILL);
        //只绘制重叠的部分
        progressPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);

        textPaint.setAntiAlias(true);
        textPaint.setTextSize(60);

        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmapCanvas = new Canvas(bitmap);

        MyGestureDetectorListener listener = new MyGestureDetectorListener();
        final GestureDetector detector = new GestureDetector(getContext(), listener);

        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return detector.onTouchEvent(event);
            }
        });

        setClickable(true);

    }

    class MyGestureDetectorListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // Toast.makeText(getContext(), "点击了onDoubleTap", Toast.LENGTH_SHORT).show();
            startDoubleTapAnimation();
            isSingleTag =false;
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            //Toast.makeText(getContext(), "点击了onSingleTapUp", Toast.LENGTH_SHORT).show();

            isSingleTag =true;
            currentProgress = progress;
            startSingleTapAnimation();
            return super.onSingleTapConfirmed(e);

        }

    }


    private void startSingleTapAnimation() {
        handler.postDelayed(singleTapRunable, 200);
    }

    private SingleTapRunable singleTapRunable = new SingleTapRunable();

    class SingleTapRunable implements Runnable {
        @Override
        public void run() {
            count--;
            if (count >= 0) {
                invalidate();
                handler.postDelayed(singleTapRunable, 200);
            }else {
                handler.removeCallbacks(singleTapRunable);
                count = 50;
            }
        }
    }

    private void startDoubleTapAnimation() {
        handler.postDelayed(doubleTapRunable, 50);
    }

    private DoubleTapRunable doubleTapRunable = new DoubleTapRunable();

    class DoubleTapRunable implements Runnable {
        @Override
        public void run() {
            currentProgress++;
            if (currentProgress <= progress) {
                invalidate();
                handler.postDelayed(doubleTapRunable, 50);
            } else {
                handler.removeCallbacks(doubleTapRunable);
                currentProgress = 0;
            }
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        bitmapCanvas.drawCircle(width / 2, height / 2, width / 2, circlePaint);
        path.reset();
        float y = (1 - currentProgress * 1.0f / max) * height;
        path.moveTo(width, y);
        path.lineTo(width, height);
        path.lineTo(0, height);
        path.lineTo(0, y);

        if (!isSingleTag) {
            float d = (1 - currentProgress * 1.0f / progress) * 10;

            for (int i = 0; i < 8; i++) {
                path.rQuadTo(10, -d, 20, 0);
                path.rQuadTo(10, d, 20, 0);
            }

        }else {
            float d = count*1.0f/50 *10;
            if (count%2 ==0) {
                for (int i = 0; i < 8; i++) {
                    path.rQuadTo(20, -d, 40, 0);
                    path.rQuadTo(20, d, 40, 0);
                }
            }else {
                for (int i = 0; i < 8; i++) {
                    path.rQuadTo(20, d, 40, 0);
                    path.rQuadTo(20, -d, 40, 0);
                }
            }
        }
        path.close();
        bitmapCanvas.drawPath(path, progressPaint);

        //文本
        int nowprogress = (int) (currentProgress * 1.0f / max * 100);
        String text = nowprogress + "%";
        float widtText = textPaint.measureText(text);
        float textHeightOffest = (textPaint.descent() - textPaint.ascent()) / 2 - textPaint
                .descent();
        bitmapCanvas.drawText(text, width / 2 - widtText / 2, height / 2 + textHeightOffest,
                textPaint);

        canvas.drawBitmap(bitmap, 0, 0, null);
        //super.onDraw(canvas);

    }
}
